﻿using System;
using LX.Core.Validation;
using S4Store.Domain;
using S4Store.Persistent;
using System.Linq;

namespace S4Store.Service.Valid.SellerValid
{
    public class Modify : ValidExpression<Employee>
    {
        public S4StoreDataContext PersistentContext { get; set; }

        public override void ValidEntity(ValidationResult result, Employee entity)
        {
            NoEmpty(result, "姓名", entity.Name);
            if (entity.WorkStartDate == DateTime.MinValue)
            {
                PushError(result, "请填写从业起始日期");
            }
            if (entity.Birthday == DateTime.MinValue)
            {
                PushError(result, "请填写出生年月");
            }
            if (entity.EmployedDate == DateTime.MinValue)
            {
                PushError(result, "请填写入职时间");
            }
            if (entity.StoreId == 0)
            {
                PushError(result, "请选择所属店");
            }
            if (entity.TypeValue == 0)
            {
                PushError(result, "请选择职位类别");
            }
            if (PersistentContext.Set<Employee>().AsQueryable().FirstOrDefault(t => t.Name == entity.Name && t.StoreId == entity.StoreId && t.Id != entity.Id) != null)
            {
                PushError(result, "已存在相同名称的职员");
            }
        }
    }
}
